package com.jfm.jfmc.plugin.telnet.utils;

import org.apache.commons.net.telnet.TelnetClient;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author DYang
 * @since 2024-10-12 0:31
 */
public class TelnetUtil {

    /**
     * 测试TCP 端口是否可用
     * @param host     主机
     * @param port     端口
     * @param timeout  超时时间
     */
    public static boolean isAvailable(String host, int port, int timeout){
        boolean result = false;
        TelnetClient telnetClient = null;
        try {
            telnetClient = new TelnetClient("vt200");
            telnetClient.setConnectTimeout(timeout);
            telnetClient.connect(host, port);
            result = telnetClient.isConnected();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (telnetClient != null) {
                try {
                    telnetClient.disconnect();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return result;

    }
}
